from django.shortcuts import render
from associator.apriori import Apriori
from BigDataWeb.view import get_algorithm
from BigDataWeb.view import put_algorithm
from BigDataWeb.view import read_data_source


def apriori(request):
    context = {}
    algorithm = Apriori()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "associator/1.html", context)


def select_data_source(request):
    context = {}
    algorithm = get_algorithm(request)
    read_data_source(request, algorithm)
    context["algorithm"] = algorithm
    return render(request, "associator/2.html", context)


def understand_data_set(request):
    context = {}
    algorithm = get_algorithm(request)
    algorithm.setInPutFieldName(request.POST.get("transaction_id_name"), request.POST.get("transaction_goods_name"))
    context["algorithm"] = algorithm
    return render(request, "associator/3.html", context)


def configure_parameters(request):
    context = {}
    algorithm = get_algorithm(request)
    algorithm.min_support = float(request.POST.get("min_support")) * 0.01
    algorithm.min_confidence = float(request.POST.get("min_confidence")) * 0.01
    algorithm.implent()
    algorithm.saveToExcle()
    algorithm.generateNotebookFile()
    context["algorithm"] = algorithm
    return render(request, "associator/5.html", context)
